/**
 * TODO
 * Game Logic
 * DONE - Concatenate explosions
 * -Add collisions on bombs and terrains
 * -Kill pj on explosions
 * -Kill terrain on explosions
 * -Drop items that increase stats
 * Styling
 * -Put assets on pj
 * -Animate pj
 * -Put assets on bombs
 * -Animate bombs
 * -Animate detonation
 * -Put assets on terrain
 * IA
 * -Add IA
 */


function Bomb(data){
    this.player = data.player;
    this.x = data.x;
    this.y = data.y;
    this.width = 20;
    this.height = 20;
    this.power = data.power;
    this.exploding = false;
    this.exploded = false;
    this.destroyedBombs = [];
    this.draw = function(data){
        data.ctx.fillStyle="#FFF000";
        data.ctx.fillRect(this.x,this.y,this.width,this.height);
        if(this.exploding){
            console.log(data.bombList);
            for(var i = 1; i <= this.power; i++){
                var result = i*20;
                this.destroyBombs(result,data.bombList);

                data.ctx.fillStyle="#FFF000";
                data.ctx.fillRect(this.x+result,this.y,this.width,this.height);
                data.ctx.fillRect(this.x-result,this.y,this.width,this.height);
                data.ctx.fillRect(this.x,this.y+result,this.width,this.height);
                data.ctx.fillRect(this.x,this.y-result,this.width,this.height);
                //Check collision with terrain
                //Check collision with player
                //Check collision with bomb

            }

        }
    };
    var exploding;
    this.destroyBombs = function(position,bombList){
        for(var i in bombList){
            var bomb = bombList[i];
            if((bomb.x == this.x+position && bomb.y == this.y)||
                (bomb.x == this.x-position && bomb.y == this.y)||
                (bomb.x == this.x && bomb.y+position == this.y)||
                (bomb.x == this.x && bomb.y-position == this.y)){
                bomb.exploding = true;
                this.destroyedBombs.push(bomb);
                // setTimeout(function(){
                //     bomb.exploded = true;
                // },500);
            }
        }
    };
    this.explode = setTimeout(function(){
        exploding = setInterval(function(){
            this.exploding = true;
            //this.drawExplosion();
        }.bind(this),1000/30);
    }.bind(this),4500);
    this.detonate = setTimeout(function(){
        //Explode
        //Check collisions
        //Remove
        clearInterval(exploding);
        for(var i in this.destroyedBombs){
            this.destroyedBombs[i].exploded = true;
        }
        this.exploded = true;
        console.log('exploded');
    }.bind(this),5000);
}
function Player(data) {
    // Character stats
    this.id = 'ziwei';
    this.y = 40;
    this.x = 40;
    this.width = 20;
    this.height = 20;
    this.speed = 20;
    this.refreshSpeed = 1000/20 ;
    this.bombsPlaced = [];
    this.maxBombs = 3;
    this.power = 2;
    this.draw = function(ctx){
        for(var i in this.bombsPlaced){
            var bomb = this.bombsPlaced[i];
            if(bomb.exploded) this.bombsPlaced.splice(this.bombsPlaced.indexOf(bomb),1);
            else{
                bomb.draw({ctx:ctx,bombList:this.bombsPlaced});
            }
        }
        ctx.fillStyle="#FF0000";
        ctx.fillRect(this.x,this.y,this.width,this.height);
    };
    // Map information
    this.mapWidth = data.mapSize.width;
    this.mapHeight = data.mapSize.height;
    // Movement stats
    this.lastKey = undefined;
    this.keyA = false;
    this.keyD = false;
    this.keyW = false;
    this.keyS = false;

    this.updatePosition = function(data){
        if(data.x){
            if(this.x + data.x < 0) this.x = 0;
            else if(this.x + data.x > this.mapWidth-this.width) this.x = this.mapWidth-this.width;
            else this.x += data.x;
        }
        if(data.y){
            if(this.y + data.y < 0) this.y = 0;
            else if(this.y + data.y > this.mapHeight-this.height) this.y = this.mapHeight-this.height;
            else this.y += data.y;
        }
    };
    setInterval(function(){
        if(this.keyPress){
            switch(this.lastKey){
                case 65:
                    this.updatePosition({x:-this.speed});
                    break;
                case 87:
                    this.updatePosition({y:-this.speed});
                    break;
                case 68:
                    this.updatePosition({x:this.speed});
                    break;
                case 83:
                    this.updatePosition({y:this.speed});
                    break;
            }
        }
    }.bind(this),this.refreshSpeed);
    window.addEventListener('keydown',function(e) {
        e = e || window.event;
        if(e.keyCode != 32){
            this.keyPress = true;
            this.lastKey = e.keyCode;
        }
        switch(e.keyCode){
            case 65:
                this.keyA = true;
                break;
            case 87:
                this.keyW = true;
                break;
            case 68:
                this.keyD = true;
                break;
            case 83:
                this.keyS = true;
                break;
            case 32:
                console.log("Space input");
                if(this.bombsPlaced.length < this.maxBombs){
                    console.log("Placing bomb");
                    this.bombsPlaced.push(new Bomb({
                        player:this.id,
                        x: this.x,
                        y: this.y,
                        power: this.power
                    }));
                }
                break;
        }
    }.bind(this));
    window.addEventListener('keyup',function(e) {
        e = e || window.event;
        switch(e.keyCode){
            case 65:
                this.keyA = false;
                break;
            case 87:
                this.keyW = false;
                break;
            case 68:
                this.keyD = false;
                break;
            case 83:
                this.keyS = false;
                break;
        }
        if(!this.keyA && !this.keyW && !this.keyD && !this.keyS) this.keyPress = false;
    }.bind(this));
}